/*
 * load_location.c
 *
 *  Created on: 2012/09/04
 *      Author: shenming
 */

#include <stdio.h>
#include "sqlite3.h"
#include "db_def.h"
#include "core.h"
#include "core_internal.h"
#include "Id.h"
#include "../../CCore/table/table.h"
#include "../../CCore/table/core_table.h"

/*TBL_FILEINDEX*/
static INT loadFileIndex(sqlite3 * db) {
	INT rc = 0;
	PSZ sql_get = NULL;
	const char *zTail;
	sqlite3_stmt * stmt = NULL;

	PSZ path;
	S4 time;
	U4 size;
	BOOL bFolfer;
	BOOL bExist;
	AST tu;

	sql_get = sqlite3_mprintf("SELECT * FROM %q ORDER BY TblIndex;",
			TBL_FILEINDEX);
	rc = sqlite3_prepare(db, (const char *) sql_get, -1, &stmt, &zTail);
	if (rc != SQLITE_OK || stmt == NULL ) {
		if (sql_get != NULL ) {
			sqlite3_free(sql_get);
		}
		if (stmt != NULL ) {
			sqlite3_finalize(stmt);
		}
		return COEXIT_ERROR;
	}

	stCoreFileIndexAccess.clear();

	while (sqlite3_step(stmt) == SQLITE_ROW) {
		path = (PSZ) sqlite3_column_text(stmt, FILE_FILE_PATH - 1);
		time = sqlite3_column_int(stmt, FILE_MODIFY_TIME - 1);
		size = sqlite3_column_int64(stmt, FILE_FILE_SIZE - 1);
		bFolfer = sqlite3_column_int(stmt, FILE_IS_FOLDER - 1);
		bExist = sqlite3_column_int(stmt, FILE_IS_EXISTED - 1);
		tu = sqlite3_column_int(stmt, FILE_TRANSUNIT - 1);
		stCoreFileIndexAccess.addFile_load(path, time, size, bFolfer, bExist,
				tu);
	}
	if (sql_get != NULL ) {
		sqlite3_free(sql_get);
	}
	if (stmt != NULL ) {
		rc = sqlite3_finalize(stmt);
	}
	return rc;
}

/*TBL_CODESCOPE*/
static INT loadCodeScope(sqlite3 * db) {
	INT rc = 0;
	PSZ sql_get = NULL;
	const char *zTail;
	sqlite3_stmt * stmt = NULL;

	AST tblIndex = 0;

	sql_get = sqlite3_mprintf("SELECT * FROM %q ORDER BY TblIndex;",
			TBL_CODESCOPE);
	rc = sqlite3_prepare(db, (const char *) sql_get, -1, &stmt, &zTail);
	if (rc != SQLITE_OK || stmt == NULL ) {
		if (sql_get != NULL ) {
			sqlite3_free(sql_get);
		}
		if (stmt != NULL ) {
			sqlite3_finalize(stmt);
		}
		return COEXIT_ERROR;
	}

	stCoreCodeScpAccess.clear();

	while (sqlite3_step(stmt) == SQLITE_ROW) {
		stCoreCodeScpAccess.alloc(1);
		tblIndex = sqlite3_column_int(stmt, P_TABLE_INDEX - 1);

		if (tblIndex != stCoreCodeScpAccess.getMax()) {
			rc = COEXIT_ERROR;
			break;
		}

		stCoreCodeScpAccess.fileindex.set(tblIndex,
				sqlite3_column_int(stmt, SCOPE_FILE_INDEX_INFO - 1));

		stCoreCodeScpAccess.startLine.set(tblIndex,
				sqlite3_column_int(stmt, SCOPE_BEGIN_LINE - 1));

		stCoreCodeScpAccess.endLine.set(tblIndex,
				sqlite3_column_int(stmt, SCOPE_END_LINE - 1));

		stCoreCodeScpAccess.startOffset.set(tblIndex,
				sqlite3_column_int(stmt, SCOPE_BEGIN_LINE_OFFSET - 1));

		stCoreCodeScpAccess.endOffset.set(tblIndex,
				sqlite3_column_int(stmt, SCOPE_END_LINE_OFFSET - 1));

	}
	if (sql_get != NULL ) {
		sqlite3_free(sql_get);
	}
	if (stmt != NULL ) {
		rc = sqlite3_finalize(stmt);
	}
	return rc;
}

INT DLL_EXPORT loadLocation(sqlite3 * db) {
	INT rc = 0;

	rc = loadFileIndex(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = loadCodeScope(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	return COEXIT_SUCCESS;
}
